<?php

class SearchController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        if ($this->_request->isPost()) {
            $keywords = $this->_request->getParam('query');
            $query = Zend_Search_Lucene_Search_QueryParser::parse($keywords);
            $index = Zend_Search_Lucene::open(APPLICATION_PATH . '/indexes/eventos');
            $hits = $index->find($query);
            $this->view->results = $hits;
            $this->view->keywords = $keywords;
        } else {
            $this->view->results = NULL;
        }
    }

    public function buildEventosAction()
    {
        // Create the search index
        $index = Zend_Search_Lucene::create(APPLICATION_PATH . '/indexes/eventos');
        
        $mdlEventos = new Application_Model_Eventos;
        $currentEventos = $mdlEventos->fetchAll();
        if ($currentEventos->count() > 0) {
            // Create a new search document for each evento
            foreach ($currentEventos as $ev) {
                $eventos = new Application_Model_Eventos();
                $evento = $eventos->getEventoById($ev->evId);
                
                // Create document
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::unIndexed('eventoId', $evento->evId));
                $doc->addField(Zend_Search_Lucene_Field::text('eventoTitolo', $evento->evTitolo), 'utf-8');
                $doc->addField(Zend_Search_Lucene_Field::text('eventoIntro', $evento->evIntro), 'utf-8');
                $doc->addField(Zend_Search_Lucene_Field::text('eventoIdent', $evento->evIdent), 'utf-8');
  //              $doc->addField(Zend_Search_Lucene_Field::text('eventoCorpo', $evento->evCorpo), 'utf-8');
                // Add the document to the index
                $index->addDocument($doc);
            }
        }
        // Optimize the index
        $index->optimize();
        
        // Pass the view data for reporting
        $this->view->indexSize = $index->numDocs();
    }
    
    public function searchProposteAction()
    {
        if ($this->_request->isPost()) {
            $keywords = $this->_request->getParam('query');
            $query = Zend_Search_Lucene_Search_QueryParser::parse($keywords);
            $index = Zend_Search_Lucene::open(APPLICATION_PATH . '/indexes/propostas');
            $hits = $index->find($query);
            $this->view->results = $hits;
            $this->view->keywords = $keywords;
        } else {
            $this->view->results = NULL;
        }
    }

        public function buildPropostasAction()
    {
        // Create the search index
        $index = Zend_Search_Lucene::create(APPLICATION_PATH . '/indexes/propostas');
        
        $mdlPropostas = new Application_Model_Propostas();
        $currentPropostas = $mdlPropostas->fetchAll();
        if ($currentPropostas->count() > 0) {
            // Create a new search document for each evento
            foreach ($currentPropostas as $prop) {
                $propostas = new Application_Model_Propostas();
                $proposta = $propostas->getPropostaById($prop->propId);
                
                // Create document
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::unIndexed('propId', $proposta->propId));
                $doc->addField(Zend_Search_Lucene_Field::text('propostaTitolo', $proposta->propTitolo), 'utf-8');
                $doc->addField(Zend_Search_Lucene_Field::text('propostaIntro', $proposta->propIntro), 'utf-8');
                $doc->addField(Zend_Search_Lucene_Field::text('propostaIdent', $proposta->propIdent), 'utf-8');
                // Add the document to the index
                $index->addDocument($doc);
            }
        }
        // Optimize the index
        $index->optimize();
        
        // Pass the view data for reporting
        $this->view->indexSize = $index->numDocs();
    }


}



